package com.zy.io;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Properties;

/**
 * @Author Gong
 * @Description
 * @dATE 2021/12/12 16:01
 **/
public class ProTest {
    public static void main(String[] args) {
        read();
    }
    public static void write() {
        Properties pro = new Properties();
        pro.setProperty("name", "wyl");
        pro.setProperty("pwd", "123");

        File file = new File("demo.properties");
        System.out.println(file.getAbsolutePath());
        FileOutputStream out = null;
        try {
            out = new FileOutputStream(file);
// 第二个参数是文件描述，一般写null
            pro.store(out, null);
            System.out.println("保存成功");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (out != null){
                    out.close();
                }

            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
    }
    public static void read() {
        Properties pro=new Properties();
        FileInputStream in=null;
        File file=new File("demo.properties");
        try {

            in=new FileInputStream(file);
            pro.load(in);

            System.out.println(pro.getProperty("name"));
            System.out.println(pro.getProperty("pwd"));
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if (in != null)
                    in.close();
            } catch (Exception e2) {
                e2.printStackTrace();
            }


        }
    }


}


